package com.xinghou.platform.server.api.controller;

import com.xinghou.platform.dao.main.SharedCardDAO;
import com.xinghou.platform.dao.main.SharedCardPraiseDAO;
import com.xinghou.platform.entity.req.ListReq;
import com.xinghou.platform.entity.req.ShareInfoReq;
import com.xinghou.platform.entity.resp.ShareInfoListResp;
import com.xinghou.platform.entity.resp.ShareInfoPhotoResp;
import com.xinghou.platform.entity.resp.ShareInfoResp;
import com.xinghou.platform.model.main.SharedCard;
import com.xinghou.platform.server.api.bussiness.MsgInfoBusinessService;
import com.xinghou.platform.server.api.bussiness.ShareInfoBusinessService;
import com.xinghou.platform.server.api.bussiness.ShopInfoBusinessService;
import com.xinghou.platform.util.ConfigUtil;
import com.xinghou.platform.util.JsonUtil;
import com.xinghou.platform.util.StringUtils;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分享信息处理
 *
 * @author xuzhen
 */
@Controller
public class ShareInfoController extends BaseControl {
    private String key = ConfigUtil.getProperty("HTTP_SERVICE_KEY");

    private String reqjson;//请求json
    private String repjson;//返回json

    private ShareInfoReq shareInfoReq;
    private ShareInfoResp shareInfoResp;
    private ShareInfoListResp shareInfoListResp;
    private ShareInfoPhotoResp shareInfoPhotoResp;

    private boolean bool = true;

    @Resource
    private ShareInfoBusinessService shareInfoBusinessService;

    @Resource
    private ShopInfoBusinessService shopInfoBusinessService;

    @Resource
    private SharedCardPraiseDAO sharedCardPraiseDAO;

    @Resource
    private MsgInfoBusinessService msgInfoBusinessService;

    @Resource
    private SharedCardDAO sharedCardDAO;







    /*
     * 发布信息删除
     *
     * @param sign
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delShareInfo", method = {RequestMethod.POST})
    public String delShareInfo( @RequestBody ShareInfoReq shareInfoReq,
                               HttpServletRequest request, HttpServletResponse response) {

        Map<String, String> requestContent = new HashMap<String, String>();
        ShareInfoResp shareInfoResp = new ShareInfoResp();
        boolean bool = true;
        try {
                shareInfoResp.setResult("0");
                if (StringUtils.isBlank(requestContent.get("userid"))) {
                    bool = false;
                    throw new ServiceException(DBExceptionEnums.USER_IS_NULL);
                } else if (StringUtils.isBlank(requestContent.get("shareid"))) {
                    bool = false;
                    throw new ServiceException(DBExceptionEnums.SHARE_ID_IS_NULL);
                } else {
                    bool = true;
                }

                if (bool) {
                    int shareid = -1;


                    if (requestContent.get("sharetype").equals("1")) {
                        shareid = shareInfoBusinessService.updateShareCardState(shareInfoReq);
                    }

                    if (shareid == 1) {
                        shareInfoResp.setCkresult("0");
                        shareInfoResp.setErrcode("0");
                        shareInfoResp.setErrmsg("success");
                    } else {
                        throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
                    }
                }


        } catch (Exception e) {
            //e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }

        repjson = JsonUtil.toJson(shareInfoResp);

        return response(shareInfoResp);
    }

    /*
     * 统计处理
     *
     * @param sign
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/praiseCount", method = {RequestMethod.POST})
    public String praiseCount(@RequestBody ShareInfoReq shareInfoReq,
                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> requestContent = new HashMap<String, String>();
        Integer follow=null;
        ShareInfoResp shareInfoResp = new ShareInfoResp();
                if (StringUtils.isBlank(shareInfoReq.getUserid())) {
                    throw new ServiceException(DBExceptionEnums.USER_IS_NULL);
                } else if (StringUtils.isBlank(shareInfoReq.getToUserid())) {
                    throw new ServiceException(DBExceptionEnums.SHARE_ID_IS_NULL);
                }
                    int shareid = -1;
                    if (shareInfoReq.getSharetype().equals("4")) {
                        //关注，取消关注
                        shareid = shareInfoBusinessService.saveRelation(shareInfoReq);
                    } else if (shareInfoReq.getSharetype().equals("6")) {
                            shareid = shareInfoBusinessService.saveVisits(shareInfoReq);
                    }
             follow=shareInfoBusinessService.getisfollow(shareInfoReq.getUserid(),shareInfoReq.getToUserid());



        shareInfoResp.setIsfollow(follow+"");
        repjson = JsonUtil.toJson(follow);
        return response(shareInfoResp);
    }


    /*
     * 分享信息查询详情
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findShare", method = {RequestMethod.POST})
    public String findShare( @RequestBody ShareInfoReq shareInfoReq,
                            HttpServletRequest request, HttpServletResponse response) {

        ShareInfoResp  shareInfoResp = new ShareInfoResp();
        Boolean bool = true;
        try {
            if (StringUtils.isBlank(shareInfoReq.getUserid())) {
                bool = false;
                throw new ServiceException(DBExceptionEnums.USER_IS_NULL);
            } else if (StringUtils.isBlank(shareInfoReq.getShareid())) {
                bool = false;
                throw new ServiceException(DBExceptionEnums.SHARE_ID_IS_NULL);
            } else if (StringUtils.isBlank(shareInfoReq.getSharetype())) {
                bool = false;
                throw new ServiceException(DBExceptionEnums.SHARE_TYPE_IS_NULL);
            } else {
                bool = true;
            }

            if (bool) {
                shareInfoResp = shareInfoBusinessService.getShareInfo(shareInfoReq);
                shareInfoResp.setResult("0");
                shareInfoResp.setPhotourllist(shareInfoBusinessService.getShareInfoPhoto(shareInfoReq));

                if (shareInfoResp.getUserid().toString().equals(shareInfoReq.getUserid().toString())) {
                    shareInfoResp.setDelTag("1");
                } else {
                    shareInfoResp.setDelTag("0");
                }

                //查询门店和用户的距离
                if (shareInfoReq.getSharetype().equals("1")) {
                    shareInfoResp.setDistance(shopInfoBusinessService.distanceShopAndUser(shareInfoReq.getUserid(), shareInfoResp.getShopid()));
                } else {
                }
            }
        } catch (Exception e) {
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }

        repjson = JsonUtil.toJson(shareInfoResp);
        return response(shareInfoResp);
    }


    //----------------------------------------NEW---------------------------------------

    /***
     * 发布卡分享
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/sendCard", method = {RequestMethod.POST})
    public String sendNewCard( @RequestBody ShareInfoReq shareInfoReq,
                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> requestContent = new HashMap<String, String>();
        ShareInfoResp shareInfoResp = new ShareInfoResp();
        boolean bool = false;
                if (StringUtils.isBlank(shareInfoReq.getUserid())) {
                    throw new ServiceException(DBExceptionEnums.USER_IS_NULL);
                }   else if (StringUtils.isBlank(shareInfoReq.getMoney())) {
                    throw new ServiceException(DBExceptionEnums.USER_MONEY_IS_NULL);
                } else if (StringUtils.isBlank(shareInfoReq.getMinsale())) {
                    throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
                }  else if (StringUtils.isBlank(shareInfoReq.getTransactiontype())) {
                    throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
                }

                    int shareid = shareInfoBusinessService.addShareCard(shareInfoReq);
                    if (shareid > 0) {
                      //  msgInfoBusinessService.addSystemNote(Integer.valueOf(shareInfoReq.getUserid()), "", 10);
                    } else {
                        throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
                    }
        shareInfoResp.setShareid(shareid+"");
        repjson = JsonUtil.toJson(shareInfoResp);
        return response(shareInfoResp);
    }


    /***
     * 修改
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateCard", method = {RequestMethod.POST})
    public String updateCard( @RequestBody ShareInfoReq shareInfoReq,
                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> requestContent = new HashMap<String, String>();
        ShareInfoResp shareInfoResp = new ShareInfoResp();
        boolean bool = false;


        int shareid = shareInfoBusinessService.updateShareCard(shareInfoReq);

        return response(null);
    }


    @ResponseBody
    @RequestMapping(value = "/searchShareList", method = {RequestMethod.POST})
    public String searchShareList(@RequestBody ShareInfoReq shareInfoReq,
                                   HttpServletRequest request, HttpServletResponse response){
        List<ShareInfoResp> shareInfoRespList = new ArrayList<ShareInfoResp>();
        ShareInfoListResp  shareInfoListResp = new ShareInfoListResp();
        try {
            if(StringUtils.isBlank(shareInfoReq.getUserid())){
                throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
            }
            shareInfoRespList = shareInfoBusinessService.searchShareList(shareInfoReq);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }
        repjson = JsonUtil.toJson(shareInfoRespList);
        return response(shareInfoRespList);
    }



    @ResponseBody
    @RequestMapping(value = "/findNewSharelist", method = {RequestMethod.POST})
    public String findNewShareList(@RequestBody ShareInfoReq shareInfoReq,
                                   HttpServletRequest request, HttpServletResponse response){
        ShareInfoListResp shareInfoListResp = new ShareInfoListResp();
        try {
            if(StringUtils.isBlank(shareInfoReq.getUserid())){
                throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
            }
            System.out.println("findNewSharelist方法");
                int flag = 0;
            shareInfoReq.setType("3");
                List<ShareInfoResp> shareInfoRespList = new ArrayList<ShareInfoResp>();
                shareInfoRespList = shareInfoBusinessService.getShareInfoListNew(shareInfoReq,flag);

                if(shareInfoRespList.size()>0){
                    shareInfoListResp.setFromno(shareInfoRespList.get(shareInfoRespList.size()-1).getFromno());
                }
                shareInfoListResp.setDatalist(shareInfoRespList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }
        repjson = JsonUtil.toJson(shareInfoListResp);
        return response(shareInfoListResp);
    }



    @ResponseBody
    @RequestMapping(value = "/findNewShare", method = {RequestMethod.POST})
    public String findNewShare( @RequestBody ShareInfoReq shareInfoReq,
                               HttpServletRequest request, HttpServletResponse response) {
        ShareInfoResp shareInfoResp = new ShareInfoResp();
        try {
            if (StringUtils.isBlank(shareInfoReq.getUserid())) {
                throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
            } else if (StringUtils.isBlank(shareInfoReq.getShareid())) {
                throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
            }
            shareInfoResp = shareInfoBusinessService.getShareInfoNew(shareInfoReq);

        } catch (Exception e) {
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }
        repjson = JsonUtil.toJson(shareInfoResp);
        return response(shareInfoResp);
    }

    @ResponseBody
    @RequestMapping(value="/delShare",method = {RequestMethod.POST})
    public String deleteShare(@RequestBody ShareInfoReq shareInfoReq,HttpServletRequest request,HttpServletResponse reponse)
    {
        ShareInfoResp resp=new ShareInfoResp();
        shareInfoBusinessService.deleteShare(shareInfoReq);
        if(StringUtils.isEmpty(shareInfoReq.getShareid()))
            throw new ServiceException(DBExceptionEnums.SHARE_ID_IS_NULL);
        return response(null);
    }


    @ResponseBody
    @RequestMapping(value="/cardList",method = {RequestMethod.POST})
    public String cardList(@RequestBody ListReq listReq,HttpServletRequest request,HttpServletResponse reponse)
    {
        List<SharedCard> list= shareInfoBusinessService.cardList(listReq);
        return response(list);
    }

    @ResponseBody
    @RequestMapping(value="/cardAudit",method = {RequestMethod.POST})
    public String cardAudit(@RequestBody ListReq listReq,HttpServletRequest request,HttpServletResponse reponse)
    {
        shareInfoBusinessService.cardAudit(listReq);
        return response(null);
    }


    public String getReqjson() {
        return reqjson;
    }

    public void setReqjson(String reqjson) {
        this.reqjson = reqjson;
    }

    public String getRepjson() {
        return repjson;
    }

    public void setRepjson(String repjson) {
        this.repjson = repjson;
    }

    public ShareInfoReq getShareInfoReq() {
        return shareInfoReq;
    }

    public void setShareInfoReq(ShareInfoReq shareInfoReq) {
        this.shareInfoReq = shareInfoReq;
    }

    public ShareInfoResp getShareInfoResp() {
        return shareInfoResp;
    }

    public void setShareInfoResp(ShareInfoResp shareInfoResp) {
        this.shareInfoResp = shareInfoResp;
    }

    public ShareInfoListResp getShareInfoListResp() {
        return shareInfoListResp;
    }

    public void setShareInfoListResp(ShareInfoListResp shareInfoListResp) {
        this.shareInfoListResp = shareInfoListResp;
    }

    public ShareInfoPhotoResp getShareInfoPhotoResp() {
        return shareInfoPhotoResp;
    }

    public void setShareInfoPhotoResp(ShareInfoPhotoResp shareInfoPhotoResp) {
        this.shareInfoPhotoResp = shareInfoPhotoResp;
    }
}
